package dkbta.ontology.elements.primitive;

import java.util.Arrays;
import java.util.Set;

import dkbta.ontology.Ontology;
import dkbta.ontology.elements.ElementDef;
import dkbta.ontology.elements.ElementType;
import dkbta.ontology.loading.OntologyException;
import dkbta.ontology.values.ScaledValues;
import dkbta.processing.Patient;

/**
 * Represents a primitive element definition
 * 
 * @author Uri Kanonov
 */
public class PrimitiveDef extends ElementDef{
	/**
	 * Initializes a Primitive with the given name, component type and possible values
	 * 
	 * @param name The element's name
	 * @param componentType The element's component type
	 * @param possibleValues The possible values the primitive can have
	 */
	public PrimitiveDef(String name, String componentType, ScaledValues possibleValues){
		super(ElementType.PRIMITIVE, name, componentType, possibleValues);
	}

	@Override
	public void validate(Ontology ontology) throws OntologyException{
		// If the primitive was created, it's valid
	}

	@Override
	public Primitive newInstance(long startTime, long endTime, String[] value){
		Object validatedValue;
		if (value.length == 0
				|| (validatedValue = getValues().parseAndValidate(value[0])) == null){
			throw new IllegalArgumentException("Invalid value: " + Arrays.toString(value));
		}
		return new Primitive(_name, startTime, endTime, validatedValue, getValueType());
	}

	@Override
	public void addRequiredPrimsAndEvents(Set<ElementDef> requiredPrimsAndEvents){
		requiredPrimsAndEvents.add(this);
	}

	@Override
	public void compute(Patient p){
		throw new UnsupportedOperationException("Primitives aren't meant to be computed");
	}
}
